package com.fand.manager.controller.advice;


import com.fand.common.exception.BusinessException;
import com.fand.common.util.ResultHelp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * Author: zhangjiabin
 * Email: javaboyok@163.com
 * CreateDate: 2021/5/16
 * Version: 1.0
 */
@Slf4j
@RestControllerAdvice
public class ControllerExceptionHandler {


    @ExceptionHandler(Exception.class)
    public ResultHelp hanlderException(Throwable throwable) {
        log.error("服务异常：throwable", throwable);
        return ResultHelp.error("请求丢了，程序员正在解决", 500);
    }

    @ExceptionHandler(BusinessException.class)
    public ResultHelp hanlderBusinessException(BusinessException ex) {
        log.error("服务异常：throwable", ex);
        return ResultHelp.error(ex.getMessage(), 400);
    }

    @ExceptionHandler(AccessDeniedException.class)
    public ResultHelp hanlderAccessDeniedException(Throwable throwable) {
        log.error("服务异常：throwable", throwable);
        return ResultHelp.error("没有权限，请联系管理员", 403);
    }
}
